<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp" %>
<%
    String uuid = UUID.randomUUID().toString();
    request.setAttribute("uuid", uuid);
%>
<form  id="wordNumberEleForm" class="container-fluid am-margin-top">
	<input type="hidden" name="id"/>
	<input type="hidden" name="wnId"/>
	<input type="hidden" name="type" value="2"/>
	<input type="hidden" name="reg" />
	
	<div class="form-group col-sm-12">
         <label for="title">参考值</label>
       	<select name="refer" class="form-control">
			<option value="1" selected="selected">产生顺序</option>
			<option value="2">根据用户，产生不同的流水号</option>
			<option value="3">根据组织机构代码，产生不同的流水号</option>
			<option value="4">根据行政区划代码，产生不同的流水号</option>
			<option value="5">根据年份，产生不同的流水号</option>
			<option value="6">根据月份，产生不同的流水号</option>
			<option value="7">根据自定义参数生成</option>
		</select>
     </div>
	 <div class="form-group col-sm-12">
         <label for="title">流水号长度</label>
         <input type="text" name="numLength" class="form-control">
     </div>
     <div class="form-group col-sm-12">
         <label for="title">是否补零</label>
         <input type="checkbox" checked="checked" name="isSetZero">
     </div>
     <div class="form-group col-sm-12">
         <label for="title">表达式</label>
         <input type="text" name="d" value="0000" readonly="readonly" class="form-control">
     </div>
     
	<div class="form-group pull-right">
         <a href="javascript:void(0)" id="wordNumberEleSubmitBt"  class="btn btn-primary">保存</a>
     </div>
</form>
<script type="text/javascript">
$(function(){
	var uuid = '${uuid}';
    var id = '${param.id}';
    var wordNumberEleForm=$("#wordNumberEleForm");
    
    if(id){
    	$.getJSON(ctx + '/wordNumberAct/findWordNumberEleById.do', {id: id}, function (data) {
            webUtils.fullForm({
                form: wordNumberEleForm,
                data: data
            });
            
            if(data.reg){
            	var reg=eval("("+data.reg+")");
            	if(!!reg){
            		if(reg.d){
            			wordNumberEleForm.find("input[name=d]").val(reg.d);
            			wordNumberEleForm.find("input[name=numLength]").val(reg.d.length);
            			if(reg.d.indexOf("#")>=0){
            				wordNumberEleForm.find("input[name=isSetZero]").attr("checked",false);
            			}
            		}
            		if(reg.refer){
            			wordNumberEleForm.find("select[name=refer]").find("option[value="+reg.refer+"]").attr("selected", true);
            		}
            	}
            }
        });
    }else{
    	var wnId='${param.wnId}';
    	wordNumberEleForm.find("input[name=wnId]").val(wnId);
    }
    
    wordNumberEleForm.find("input[name=numLength]").blur(function(){
		setDReg();
	});
    wordNumberEleForm.find("input[name=isSetZero]").click(function(){
		setDReg();
	});
	
    
	$("#wordNumberEleSubmitBt").click(function(){
		setDReg();
		var r="{d:'"+wordNumberEleForm.find("input[name=d]").val()+"',refer:"+wordNumberEleForm.find("select[name=refer]").val()+"}";
		wordNumberEleForm.find("input[name=reg]").val(r);
		
		wordNumberEleForm.ajaxSubmit({
            url: ctx + '/wordNumberAct/saveWordNumEle.do',
            data: {},
            type: "POST",
            success: function (data) {
                if (data.success) {
                    webUtils.alert("提交成功");
                    top.webUtils.topicPublish("wordNumberEle.edit.success", data);
                } else {
                	webUtils.error(data.msg);
                }
            }
        });
	});
	
	function setDReg(){
		var numLength=wordNumberEleForm.find("input[name=numLength]").val();
		var isSetZero=wordNumberEleForm.find("input[name=isSetZero]").attr("checked");
		var base=isSetZero?"0":"#";
		var r="";
		for(var i=0;i<numLength;i++){
			r+=base;
		}
		wordNumberEleForm.find("input[name=d]").val(r);
	}
})
</script>